package cn.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.bean.Log;
import cn.bean.Permission;
import cn.bean.User;
import cn.dao.LogDao;
import cn.dbc.BaseDao;

public class LogDaoImpl implements LogDao
{

	@Override
	public Log findLogById(int id)
	{
		Log l=new Log();
		BaseDao bd=new BaseDao();
		List<Object> lo=new ArrayList<Object>();
		lo.add(id);
		l=(Log) bd.find("select * from log where id=?", lo, "cn.bean.Log").get(0);
		return l;
	}

	@Override
	public Log doLog(String account, String password)
	{
		Log l=new Log();
		BaseDao bd=new BaseDao();
		List<Object> lo=new ArrayList<Object>();
		lo.add(account);
		lo.add(password);
		bd.update("insert into log(account,password) values(?,?)", lo);
		l.setId(findLogByAcc(account).getId());
		l.setAccount(account);
		l.setPassword(password);
		return l;
	}

	@Override
	public boolean updateLogbyUser(User user, String password)
	{
		boolean isFlag=false;
		BaseDao bd=new BaseDao();
		List<Object> lo=new ArrayList<Object>();
		lo.add(password);
		lo.add(user.getLog().getId());;
		isFlag=bd.update("update log set password=? where id=?", lo);
		return isFlag;
	}


	@Override
	public Log findLogByAcc(String account)
	{
		Log l=new Log();
		BaseDao bd=new BaseDao();
		List<Object> lo=new ArrayList<Object>();
		lo.add(account);
		l=(Log) bd.find("select * from log where account=?", lo, "cn.bean.Log").get(0);
		return l;
	}

	@Override
	public List<Log> findAllLog()
	{
		List<Log> ll=new ArrayList<Log>();
		BaseDao bd=new BaseDao();
		List<Object> lo=new ArrayList<Object>();
		List<Object> lLog=new ArrayList<Object>();
		lLog=bd.find("select * from log", lo, "cn.bean.Log");
		for(Object o:lLog)
		{
			Log l=(Log) o;
			ll.add(l);
		}
		return ll;
	}

}
